/**
 * 
 */
package com.jolestar.yayaweibotong.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jolestar.yayaweibotong.dao.UserDomain;
import com.jolestar.yayaweibotong.dao.UserDomain.SyncType;
import com.jolestar.yayaweibotong.util.RequestUtils;

/**
 * @author jolestar@gmail.com
 * 
 */
public class UpdateCfgAction extends AbstractAction {

	public UpdateCfgAction() {
		super("update-cfg");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jolestar.yayaweibotong.controller.AbstractAction#doAction(javax.servlet
	 * .http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected String doAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String masterKey = request.getParameter("master");
		HttpSession session = request.getSession(true);
		UserDomain user = (UserDomain) session.getAttribute("user");
		if (masterKey != null) {
			user.setMasterAccount(masterKey);
		}
		String[] activityKeys = request.getParameterValues("activity");
		if (activityKeys != null) {
			user.setActivityAccounts(activityKeys);
		}
		int syncType = RequestUtils.getIntParam(request, "sync_type", 0);;
		user.setSyncType(SyncType.fromInt(syncType));
		user.save();
		session.setAttribute("user", user);
		return "redirect:/dashboard";
	}

	@Override
	public String getPath() {
		return "/" + this.getName();
	}

}
